<< define "role/edit.html" >>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>编辑角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/mbgadmin.css">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/mbgadmin.js"></script>
</head>
<body>
<form class="layui-form" lay-filter="roles_edit_form">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                   class="layui-input" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色描述</label>
        <div class="layui-input-block">
            <input type="text" name="describe" placeholder="请输入角色描述" autocomplete="off"
                   class="layui-input" value="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="启用" checked>
            <input type="radio" name="status" value="0" title="禁用" >
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色菜单<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <div id="menus"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addRole" id="submit">立即提交</button>
        </div>
    </div>
</form>
<script>
    function child(dataFromFather) {
    layui.use(['tree', 'jquery', 'form','layer'], function () {
        var tree = layui.tree;
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        form.val('roles_edit_form', {
            "name": dataFromFather.Name,
            "describe": dataFromFather.describe,
        });
        var status =dataFromFather.isUsable
        $("input[name='status'][value='" + status + "']").prop("checked", true);
        var tree_data =[]
        $.get("/roles/edit/treelist/"+dataFromFather.ID,function (res) {
            //渲染
            var  data={
                elem: '#menus'  //绑定元素
                , id: 'editmenus'
                , showCheckbox: true
                ,data:res.data
            }
            tree_data = res.data
            tree.render(data);
            // 如果是超级管理员权限就设置所有节点选中状态
            if (dataFromFather.ID === 1){
                function setAllNodesChecked(nodes) {
                    layui.each(nodes, function (index, node) {
                        tree.setChecked('editmenus', node.id); // 设置选中状态
                        if (node.children && node.children.length > 0) {
                            setAllNodesChecked(node.children); // 递归处理子节点
                        }
                    });
                }

                setAllNodesChecked(res.data); // 设置所有节点为选中状态

            }else {
                tree.setChecked('editmenus', res.ids)
            }
        })

        form.on('submit(addRole)', function (data) {
            $("#submit").addClass("layui-btn-disabled") //防止连续点击添加键。
            if (dataFromFather.ID === 1) {
                layer.msg("超级管理员权限不允许修改",function (){
                    $("#submit").removeClass("layui-btn-disabled")
                })
            }else {
            // 调用上面的函数来获取选中节点的ID
            var checkedIds = [];
            var assetsIds = []
            getSelectedNodeIds(tree.getChecked('editmenus'), assetsIds);
                var checkedData = layui.tree.getChecked('editmenus');
                extractIds(checkedData,checkedIds);
                let loadIndex = layer.load(2);
                $.post("/roles/edit/"+dataFromFather.ID, JSON.stringify({ menuIDs: checkedIds,name:data.field.name,describe:data.field.describe,status:data.field.status,assetsIds:assetsIds}), function(data) {
                    if (data.code===0){
                        layer.close(loadIndex)
                        parent.layer.msg(data.msg);
                        ReloadLists()
                    }else {
                        layer.close(loadIndex)
                        layer.msg(data.msg)
                        $("#submit").removeClass("layui-btn-disabled")
                    }
                })
            }
            return false;
        })
    });
    }
    function getSelectedNodeIds(treeData, checkedIds) {
        layui.each(treeData, function (index, node) {
            if (node.children && node.children.length > 0) {
                // 如果节点有子节点，递归处理子节点
                getSelectedNodeIds(node.children, checkedIds);
            } else if (node.checked) {
                // 如果节点被选中且没有子节点，添加其ID到checkedIds数组中
                checkedIds.push(node.id);
            }
        });
    }
    function extractIds(data, ids) {
        for (var i = 0; i < data.length; i++) {
            var id = data[i].id;
            ids.push(id);
            if (data[i].children && data[i].children.length > 0) {
                extractIds(data[i].children, ids);
            }
        }
    }
</script>
</body>
</html>
<< end >>